package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.*;
import com.tbit.uqbike.client.dao.core.ParkFeatureDao;
import com.tbit.uqbike.client.pojo.MqData;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.pojo.newEntity.ParkFeature;
import com.tbit.uqbike.client.pojo.newEntity.StepLog;
import com.tbit.uqbike.client.service.CacheService;
import com.tbit.uqbike.client.service.ParkFeatureService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:35 2021/3/5
 */
@Service
public class ParkFeatureServiceImpl implements ParkFeatureService {

    @Autowired
    private ParkFeatureDao parkFeatureDao;
    @Autowired
    private CacheService cacheService;

    @Override
    public Integer check(Integer parkPointId, Integer blePointId, String machineNO, Integer accountId, String orderNO) {
        Integer button = null;
        AccountConfigExt accountConfigExt = cacheService.getAccountConfigExtByKey(accountId,
                AccountConfigExtConstant.ACCOUNTCONFIG_BLE);
        if (accountConfigExt != null) {
            button = Integer.valueOf(accountConfigExt.getParamValue());
        }
        /**兼容总开关*/
        if (blePointId != null && (button == null || button.equals(1))) {
            /**道钉绑定站点，并且开启道钉功能*/
            ParkFeature parkFeatureDB = getByParkPointId(blePointId, ParkFeatureConstant.PARKFEATURE_TYPE_BLE);
            if (parkFeatureDB != null && parkFeatureDB.getEnable() == 1) {
                return blePointId;
            } else {
                /**道钉绑定站点没有开启道钉功能，车辆位置站点开启道钉功能，还车失败*/
                if (parkPointId != null) {
                    ParkFeature parkFeature1 = getByParkPointId(parkPointId, ParkFeatureConstant.PARKFEATURE_TYPE_BLE);
                    if (parkFeature1 != null && parkFeature1.getEnable() == 1) {
                        return null;
                    }
                }
                return parkPointId;
            }
        } else {
            /**如果车辆所在站点有开启道钉功能，无法还车*/
            if (parkPointId != null && (button == null || button.equals(1))) {
                ParkFeature parkFeatureDB = getByParkPointId(parkPointId, ParkFeatureConstant.PARKFEATURE_TYPE_BLE);
                if (parkFeatureDB != null && parkFeatureDB.getEnable() == 1) {
                    return null;
                }
            }
            return parkPointId;
        }
    }

    @Override
    public ParkFeature getByParkPointId(Integer parkPointId, Integer type) {
        ParkFeature parkFeature = new ParkFeature();
        parkFeature.setParkPointId(parkPointId);
        parkFeature.setFeatureType(type);
        ParkFeature parkFeatureDB = parkFeatureDao.getByParkPointId(parkFeature);
        return parkFeatureDB;
    }

    /**
     * 添加日志
     */
    public void addStepLog(Integer accountId, String branch, String machineNO, String orderNO, String remark, Integer result, Integer type) {
        StepLog stepLog = new StepLog();
        stepLog.setAccountId(accountId);
        stepLog.setAddTime(DateTimeUtil.getNowTime());
        stepLog.setBranch(branch);
        stepLog.setMachineNO(machineNO);
        stepLog.setOrderNO(orderNO);
        stepLog.setRemark(remark);
        stepLog.setResult(result);
        stepLog.setType(type);
        MqData mqData = new MqData();
        mqData.setFeedback(RabbitMQData.aiQueue);
        mqData.setMsgId(GatewayProtocol.MQ_MSG_ID_AROUND_STEP_PUSH);
        mqData.setData(stepLog);
        /**放入内存，定时推送*/
        SystemData.pushList.add(mqData);
    }
}
